package ext.vpde.cadvaulting.migration;

import java.util.*;
import java.io.*;
import com.infoengine.SAK.IeService;
import com.infoengine.SAK.Task;
import com.infoengine.au.NamingService;

import ext.vpde.cadvaulting.util.WINDSQL;
import wt.util.WTProperties;

public class LoadContent {
	public static void main ( String [] args ) {
		if (args.length != 4) {
			System.out.println("\nUsage: java.ext.vpde.cadvaulting.LoadContent adminid adminpw loadfile contentdir");
		} else {
			if (LoadUtil.loginRemote(args[0],args[1])) {
			try {
				  System.out.println("BEGIN LOAD EPM CONTENT FOR " + args[3] + ": " + java.util.Calendar.getInstance().getTime());
				  System.out.println("READING: " + args[2]);
				  try {
					WINDSQL.openConnection();
					String inFile = args[2];
					BufferedReader file = new BufferedReader(new FileReader(inFile));
					String s = file.readLine();
					int lineNumber = -1;
					while (s!= null && s.length() > 0) {	
						LoadUtil.printProgress(++lineNumber);
						StringTokenizer strTok = new StringTokenizer(s,"|");
						String docName = strTok.nextToken();
						String docType = strTok.nextToken();
						String docVer  = strTok.nextToken();
						String docIter = strTok.nextToken();
						String state   = strTok.nextToken();
						String desc    = strTok.nextToken();
						String fPath   = strTok.nextToken();

						if (!fPath.equals("none")) {
							fPath = fPath.substring(3);
							if (WINDSQL.documentExists(docName,docVer,docIter)) {
								//System.out.println("LOADING CONTENT FOR: " + docName + " " + docVer + "." + docIter);
								String obid   = "OR:wt.epm.EPMDocument:" + WINDSQL.getDocument(docName,docVer,docIter);
								String urlstr = "file://" + args[3] + "/" + fPath;
								//System.out.println(urlstr);
								
								runLoadTask(args[0],args[1],obid,urlstr);
				
							} else {
								System.out.println("CAD DOC: " + docName + ", " + docVer + "." + docIter + " NOT FOUND");
							}
						}
						s = file.readLine();
				  	}
					WINDSQL.closeConnection();
				  } catch (Exception e) { System.out.println("EXCEPTION IN LOAD CONTENT: " + e.getLocalizedMessage()); }
				  System.out.println("-END- LOAD EPM CONTENT: " + java.util.Calendar.getInstance().getTime());
			} catch(Exception e) {
				e.printStackTrace();
			}
			}
		}
    }
	
	public static void runLoadTask (String userid, String userpw, String obid, String urlstr) {
		try {
			WTProperties wtProps = WTProperties.getLocalProperties();
			String ADAPTER       = wtProps.getProperty("wt.federation.ie.VMName");
			String RESOURCE      = wtProps.getProperty("wt.federation.ie.propertyResource");
			String NAMINGSERVICE = wtProps.getProperty("wt.federation.ie.namingService");
			
			Task t = new Task("ext/vpde/cadvaulting/migration/AddContent.xml");
			NamingService.setVMName ("remote");
			NamingService.newInstance (NAMINGSERVICE,RESOURCE,null);
			IeService ie = new IeService();
			ie.setEnableExceptions ( true );
			ie.setCredentials(userid, userpw);
			Vector processors=new Vector();
			processors.add(ADAPTER);
			t.setProcessors(processors);
			t.setUsername(userid);
			t.setService (ie);
			t.addParam("DBUSER",userid);
			t.addParam("PASSWD",userpw);
			t.addParam("OBID",obid);
			t.addParam("PATH", urlstr);
			t.invoke();
			System.out.println("LOADING " + urlstr);
		} catch(Exception e) { e.printStackTrace();	}
    }
}